<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_csv_CsvParser
{
	private static $instance;
	private $fileHandler;
	private $delimiter = ",";
	private $length = 1000;
	private $parsedRows = array();
	private $parsedCols = array();
	private $rowsNumber;
	private $colsNumber;
	
	/**
	 * @return f_csv_CsvParser
	 */
	private function __construct()
	{
		return $this;
	}
	
	/**
	 * Get singleton instance
	 *
	 * @return f_csv_CsvParser
	 */
	public static function getInstance()
	{
		if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	/**
	 * Set the file to get datas from
	 *
	 * @param string $csvFilePath
	 * @return f_csv_CsvParser
	 */
	public function fromFile($csvFilePath)
	{
		if(is_file($csvFilePath))
		{
			$this->fileHandler = fopen($csvFilePath, "r");

			$row = 1;
			while (($data = fgetcsv($this->fileHandler, $this->length, $this->delimiter)) !== FALSE) 
			{
				$num = count($data);
			
			    for ($c=0; $c < $num; $c++) 
			    {
			        $this->parsedRows[$row][] = $data[$c];
			        $this->parsedCols[$c+1][] = $data[$c];
			    }			
			    
			    $row++;
			}
			
			$this->rowsNumber = count($this->parsedRows);
			$this->colsNumber = count($this->parsedCols);

			return $this;
		}
		else
		{
			throw new Exception($csvFilePath." file not found");
		}
	}
	
	/**
	 * Return an array which contents datas by cols
	 *
	 * @return array
	 */
	public function getValuesByCols()
	{
		return $this->parsedCols;
	}
	
	/**
	 * Returns column values by column number
	 *
	 * @param integer $colNumber
	 * @return array
	 */
	public function getValuesByColNumber($colNumber)
	{
		return $this->parsedCols[$colNumber];
	}
	
	/**
	 * Returns row values by row number
	 *
	 * @param integer $rowNumber
	 * @return array
	 */
	public function getValuesByRowNumber($rowNumber)
	{
		return $this->parsedRows[$rowNumber];
	}
	
	/**
	 * Return the value from the given column and row numbers
	 *
	 * @param integer $colNumber
	 * @param integer $rowNumber
	 * @return string
	 */
	public function getValueByColAndRowNumber($colNumber, $rowNumber)
	{
		return $this->parsedCols[$colNumber][$rowNumber];
	}
	
	/**
	 * Return an array which contents datas by rows
	 *
	 * @return array
	 */
	public function getValuesByRows()
	{
		return $this->parsedRows;
	}
	
	/**
	 * Returns the number of rows in the csv file
	 *
	 * @return integer
	 */
	public function getRowsNumber()
	{
		return $this->rowsNumber;
	}
	
	/**
	 * Returns the number of columns in the csv file
	 *
	 * @return integer
	 */
	public function getColsNumber()
	{
		return $this->colsNumber;
	}	
	
	/**
	 * Set another field delimiter
	 *
	 * @param string $delimiter
	 */
	public function setDelimiter($delimiter)
	{
		$this->delimiter = $fieldSeparator;
	}
	
	/**
	 * Set another field length
	 *
	 * @param integer $length
	 */
	public function setLength($length)
	{
		$this->length = $length;
	}
}